package cn.hist.hfp.service;

import cn.hist.hfp.converter.CommodityOrderConverter;
import cn.hist.hfp.converter.PetOrderConverter;
import cn.hist.hfp.entity.*;
import cn.hist.hfp.repository.CommodityOrderRepository;
import cn.hist.hfp.repository.PetOrderRepository;
import cn.hist.hfp.vo.CommodityOrderVO;
import cn.hist.hfp.vo.PetOrderVO;
import org.aspectj.weaver.patterns.PerObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class OrderService {

    private final
    PetOrderRepository petOrderRepository;

    private final CommodityOrderRepository commodityOrderRepository;

    private final PetOrderConverter petOrderConverter;

    private final CommodityOrderConverter commodityOrderConverter;

    private final PetService petService;

    private final CommodityService commodityService;

    private final UserService userService;

    @Autowired
    public OrderService(PetOrderRepository petOrderRepository, CommodityOrderRepository commodityOrderRepository, PetOrderConverter petOrderConverter, CommodityOrderConverter commodityOrderConverter, PetService petService, CommodityService commodityService, UserService userService) {
        this.petOrderRepository = petOrderRepository;
        this.commodityOrderRepository = commodityOrderRepository;
        this.petOrderConverter = petOrderConverter;
        this.commodityOrderConverter = commodityOrderConverter;
        this.petService = petService;
        this.commodityService = commodityService;
        this.userService = userService;
    }

    public PetOrder createPetOrder(PetOrder petOrder) {
        return petOrderRepository.saveAndFlush(petOrder);
    }

    public List<PetOrderVO> findAllPetOrders() {
        return petOrderConverter.convert(petOrderRepository.findAll());
    }

    public List<PetOrderVO> findAllPetOrdersByUser(int user_id) {
        User user = userService.findOne(user_id);
        return petOrderConverter.convert(petOrderRepository.findAllByUser(user));
    }

    public CommodityOrder createCommodityOrders(CommodityOrder commodityOrder) {
        return commodityOrderRepository.saveAndFlush(commodityOrder);
    }

    public List<CommodityOrderVO> findAllCommodityOrders() {
        return commodityOrderConverter.convert(commodityOrderRepository.findAll());
    }

    public List<CommodityOrderVO> findAllCommodityOrdersByUser(int user_id) {
        User user = userService.findOne(user_id);
        return commodityOrderConverter.convert(commodityOrderRepository.findAllByUser(user));
    }
}
